package com.example.administrator.basedemos.retorfit;

import android.content.Context;

import com.example.administrator.basedemos.bean.GithubUserBean;
import com.example.administrator.basedemos.utils.LogUtil;
import com.example.administrator.basedemos.utils.TmtUtils;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 创建者     ZCL
 * 创建时间   2018/3/15 14:37
 * 描述	      ${TODO}
 * <p>
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */

public abstract class RetrofitCallBack implements retrofit2.Callback<com.example.administrator.basedemos.bean.GithubUserBean> {
    private static final String TAG = "RetrofitCallBack";

    private Context context;

    public RetrofitCallBack(Context context) {
        this.context = context;
    }

    @Override
    public  void onResponse(Call<GithubUserBean> call, Response<GithubUserBean> response){
        if(response.isSuccessful()){
            setSuccessMessage(response.body());
        }else{
            setErrorMessage(response.code(),response.errorBody(),response.message());
        }
    }

    protected  void setErrorMessage(int code, ResponseBody responseBody, String message){
        TmtUtils.midToast(context,message);
        LogUtil.util(TAG,"Retrofit Error-----code:"+code+"--------errorBody:"+responseBody+"-----------------mes:"+message);
    }

    protected abstract void setSuccessMessage(GithubUserBean body);

    @Override
    public void onFailure(Call<GithubUserBean> call, Throwable t) {
        TmtUtils.midToast(context,"Retrofit Fail!");
        LogUtil.util(TAG,"retrofit Failed!!-------------:"+t.getLocalizedMessage());
    }
}
